package com.oba.king.hospitalinfo.doctorinformation;

import java.util.ArrayList;
import java.util.List;

import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import com.oba.base.dao.DBDataModel;
import com.oba.base.service.BaseBean;
import com.oba.king.hospitalinfo.doccolinformation.DoccolInformation;
import com.oba.king.hospitalinfo.hospitalinformation.HospitalInformation;

public class DoctorInfoBean extends BaseBean<DoctorInfo,DoctorInfoService> {
	
	private DBDataModel doctorInfoList = DBDataModel.create();
	private boolean showEdit = false;
	private DoctorInfo doctorInfo; 
	private static final String ADD_NEW_DOCTOR_PATH = "/hospitalinfo/doctorInformationAdd.jspx";
	private static final String SHOW_EDIT_DOCTOR_PATH = "/hospitalinfo/doctorInformation.jspx"; 
//	String doctorNm;
	private HospitalInformation hospitalInformation;
	private List<SelectItem> hospitalList;
	private List<SelectItem> doccolInfoList;
	private DoctorCondition doctorCondition;
	
	@Override
	public void init() {
		System.out.println("Start DoctorInfoBean init()>>>>");
		this.doFind();
		doctorCondition = new DoctorCondition();
		hospitalList = new ArrayList<SelectItem>();
		doccolInfoList = new ArrayList<SelectItem>();
		doccolInfoList.add(new SelectItem(null,"全部"));
		hospitalList.add(new SelectItem(null,"全部"));
		for (HospitalInformation hosp : getService().getDao().buildAllHospital()) {
			hospitalList.add(new SelectItem(hosp, hosp.getHospitalName()));
		}
		System.out.println("End DoctorInfoBean init()<<<<");
	}
	
	@Override
	public String doFind() {
		System.out.println("Start DoctorInfoBean doFind()>>>>");
		this.doctorInfoList.find(this.getService().getDao().buildAll());
		System.out.println("Start DoctorInfoBean doFind()<<<<");		
		return null;
	}
	
	public void addNewDoctorInfo(){
		System.out.println("Start DoctorInfoBean addNewRow()>>>>");
		this.doctorInfo = new DoctorInfo();
		this.doctorInfo.setNewBo(true);
		this.goTarget(ADD_NEW_DOCTOR_PATH);
		System.out.println("Start DoctorInfoBean addNewRow()<<<<");
	}
	
	public void doEditDoctorInfo(){
		System.out.println("Start DoctorInfoBean doEditRow()>>>>");
		if(this.doctorInfoList.isRowAvailable()){
			this.doctorInfo = (DoctorInfo) this.doctorInfoList.getRowData();
			this.goTarget(ADD_NEW_DOCTOR_PATH);
		}		
		System.out.println("Start DoctorInfoBean doEditRow()<<<<");		
	}
	
	public void doDeleteDoctorInfo(){
		System.out.println("Start DoctorInfoBean doDeleteRow()>>>>");
		if(this.doctorInfoList.isRowAvailable()){
			this.getService().del((DoctorInfo)this.doctorInfoList.getRowData());
			this.doFind();
		}
		System.out.println("Start DoctorInfoBean doDeleteRow()<<<<");		
	}
	
	public void doSaveDoctorInfo(){
		System.out.println("Start DoctorInfoBean doSaveRow()>>>>");
		this.getService().save(this.doctorInfo);
//		this.showEdit = false;
		this.goTarget(SHOW_EDIT_DOCTOR_PATH);
		this.doFind();
		System.out.println("Start DoctorInfoBean doSaveRow()<<<<");	
	}
	
	/**
	 * 级连科室,此方法在页面被调用
	 */
	public void hospitalChanged(ValueChangeEvent valueChange){
		doccolInfoList.clear();
		doccolInfoList.add(new SelectItem(null,"全部"));
		for (DoccolInformation docc : getService().getDao().srchByHospital(hospitalInformation)) {
			doccolInfoList.add(new SelectItem(docc, docc.getSectionName()));
		}		
	}
	
//	/**
//	 * 根据医生的名字来模糊查找
//	 */
//	public void srchByDocName(){
//		System.out.println("Start srchByDocName doSaveRow()>>>>");
//		if(doctorNm != null && !doctorNm.trim().equals("")){
//			this.doctorInfoList.find(this.getService().getDao().srchByConditon(doctorNm),SHOW_ROW);
//		}
//		System.out.println("Start srchByDocName doSaveRow()<<<<");	
//	}
	
	/**
	 * 根据DoctorCondition模糊查询
	 */
	public void srchDoctorCondition(){
		System.out.println("Start DoctorInfoBean srchDoctorCondition()>>>>");
		if(doctorCondition != null){
			this.doctorInfoList.find(this.getService().getDao().srchDoctorByCondition(doctorCondition));
		}
		System.out.println("Start DoctorInfoBean srchDoctorCondition()>>>>");
    }	
	
	/**
	 * 点击清除按钮时调用
	 */
	public void cleanButton(){
		this.doctorCondition = new DoctorCondition();
//		this.doFind();
	}
	
	/**
	 * 页面跳转返回
	 */
	public void doRturnShow(){
		this.goTarget(SHOW_EDIT_DOCTOR_PATH);
	}
	
	/**
	 * 是否显示弹出层
	 * @return
	 */
	public boolean isShowEdit(){
		return showEdit;
	}
	
	/**
	 * 用于关闭弹出层
	 */
	public void closeShowEdit(){
		this.showEdit = false;
	}
	
	
	public DBDataModel getDoctorInfoList() {
		return doctorInfoList;
	}

	public void setDoctorInfoList(DBDataModel doctorInfoList) {
		this.doctorInfoList = doctorInfoList;
	}

	public DoctorInfo getDoctorInfo() {
		return doctorInfo;
	}

	public void setDoctorInfo(DoctorInfo doctorInfo) {
		this.doctorInfo = doctorInfo;
	}
//
//	public String getDoctorNm() {
//		return doctorNm;
//	}
//
//	public void setDoctorNm(String doctorNm) {
//		this.doctorNm = doctorNm;
//	}

	public HospitalInformation getHospitalInformation() {
		return hospitalInformation;
	}

	public void setHospitalInformation(HospitalInformation hospitalInformation) {
		this.hospitalInformation = hospitalInformation;
	}

	public List<SelectItem> getHospitalList() {
		return hospitalList;
	}

	public void setHospitalList(List<SelectItem> hospitalList) {
		this.hospitalList = hospitalList;
	}

	public List<SelectItem> getDoccolInfoList() {
		return doccolInfoList;
	}

	public void setDoccolInfoList(List<SelectItem> doccolInfoList) {
		this.doccolInfoList = doccolInfoList;
	}

	public DoctorCondition getDoctorCondition() {
		return doctorCondition;
	}

	public void setDoctorCondition(DoctorCondition doctorCondition) {
		this.doctorCondition = doctorCondition;
	}
}
